﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Diagnostics;

namespace ICA_PhysX_Interface
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void launchDemo_Click(object sender, EventArgs e)
        {
            float bowlingBallInitialVelocity = 0;
            float toolBoxMass = 0;
            float seesawDistance = 0;
            float slopeAngle = 0;
            try
            {
                bowlingBallInitialVelocity = float.Parse(textBoxBowlingBallInitialVelocity.Text);
            }
            catch
            {
                MessageBox.Show("Wrong value for the bowling ball initial velocity.");
                return;
            }
            try
            {
                toolBoxMass = float.Parse(textBoxToolBoxMass.Text);
            }
            catch
            {
                MessageBox.Show("Wrong value for the tool box mass.");
                return;
            }
            try
            {
                seesawDistance = float.Parse(textBoxDistanceSeesaw.Text);
            }
            catch
            {
                MessageBox.Show("Wrong value for the seesaw distance.");
                return;
            }
            try
            {
                slopeAngle = float.Parse(textBoxSlopeAngle.Text);
                slopeAngle = 2 * (float)3.14 * slopeAngle / 360;
            }
            catch 
            {
                MessageBox.Show("Wrong value for the slope angle.");
                return;
            }

            bool fileAlreadyExist = true;
            string filename = "exe\\physXInput.xml";
            XmlDocument physXInputDocument = new XmlDocument();
            try
            {
                //open the xml file
                physXInputDocument.LoadXml(filename);
            }
            catch
            {
                //create the xml file if it doesn't exist
                XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                xmlWriter.WriteStartElement("PhysX");

                xmlWriter.Close();
                physXInputDocument.Load(filename);
                fileAlreadyExist = false;
            }

            if (!fileAlreadyExist)
            {
                XmlNode root = physXInputDocument.DocumentElement;

                //write bowling ball velocity
                XmlElement bowlingBall = physXInputDocument.CreateElement("bowlingBallVelocity");
                XmlText velocity = physXInputDocument.CreateTextNode(bowlingBallInitialVelocity.ToString());
                root.AppendChild(bowlingBall);
                bowlingBall.AppendChild(velocity);

                //write the tool box mass
                XmlElement toolBox = physXInputDocument.CreateElement("toolBoxMass");
                XmlText mass = physXInputDocument.CreateTextNode(toolBoxMass.ToString());
                root.AppendChild(toolBox);
                toolBox.AppendChild(mass);

                //write the seesaw distance
                XmlElement seesaw = physXInputDocument.CreateElement("seesawDistance");
                XmlText distance = physXInputDocument.CreateTextNode(seesawDistance.ToString());
                root.AppendChild(seesaw);
                seesaw.AppendChild(distance);

                //write the slope angle
                XmlElement slope = physXInputDocument.CreateElement("slopeAngle");
                XmlText angle = physXInputDocument.CreateTextNode(slopeAngle.ToString());
                root.AppendChild(slope);
                slope.AppendChild(angle);

                physXInputDocument.Save(filename);
            }
            else
            {
                //modify the value if the file already exist
                XmlNode _root = physXInputDocument.DocumentElement;
                XmlNode element = _root.FirstChild;
                while (element != null)
                {
                    if (element.Name == "bowlingBallVelocity")
                        element.Value = bowlingBallInitialVelocity.ToString();
                    else if (element.Name == "toolBoxMass")
                        element.Value = toolBoxMass.ToString();
                    else if (element.Name == "seesawDistance")
                        element.Value = seesawDistance.ToString();
                    else
                        element.Value = slopeAngle.ToString();

                    element = element.NextSibling;
                }
                physXInputDocument.Save(filename);
            }
            

            //launch the demo
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo();
            p.StartInfo.Arguments = filename;
            p.StartInfo.FileName = "exe.\\demoPhysX.exe";
            p.Start();
            p.WaitForExit();
 
       
        }
    }
}
